lam1=1529;
lam2=1540;
de1=[0.808,0.807,0.813,0.812,0.814,0.809];
de2=[1.095,1.092,1.090,1.093,1.094,1.091];
c=4200;
kap1=c*de1/lam1;
kap2=c*de2/lam2;
x_position=sin(pi/4)*[0.6,1.2,1.8,2.4,3.0,3.6];

err1=10000;
err2=10000;
coefficient=[0,0,0,0,0];

for i=1:1000
y_ini=-2+2*rand(1,6);
p = polyfit(x_position, y_ini,4);
y_fit = polyval(p, x_position);
x_fine = linspace(0, 3.2, 1000); 
y_fine = polyval(p, x_fine); 
kap_return=KAPPA4(p(1),p(2),p(3),p(4),p(5),x_position);
sum3=0;
for j=1:6
    sum3=(abs(kap2(j)-kap_return(j)))^2+sum3;
end
sum3=(sum3)/6;

sum1=0;
variance1=0;
for j=1:6
    sum1=sum1+kap_return(j);
end
for k=1:6
variance1=variance1+(abs(kap_return(k)-sum1)^2/6);
end
if sum3<err1
    if variance1<err2
        err1=sum3;
        err2=variance1;
    end
     coefficient=p;
end
end

disp('曲率误差=');
disp(err1);
disp('自身误差=');
disp(err2);
disp(KAPPA4(coefficient(1),coefficient(2),coefficient(3),coefficient(4),coefficient(5),[0.3,0.4,0.5,0.6,0.7]));
figure;
plot(x_position, y_ini, 'ko', 'MarkerFaceColor', 'r', 'MarkerSize', 10);
hold on;
plot(x_fine, y_fine, 'b-', 'LineWidth', 1.5);
legend('原始数据', '拟合曲线');
title('数据点与拟合曲线');
xlabel('位置 (x)');
ylabel('位置 (y)');
hold off;